<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table @row-dblclick="getClick" :data="dictionarys" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="50">
    </el-table-column>
      <el-table-column prop="id" width="150" label="编号">
      </el-table-column>
      <el-table-column prop="sn" label="字典标签" width="480" >
      </el-table-column>
      <el-table-column prop="name" label="字典名字" width="400" >
      </el-table-column>
      <el-table-column label="操作" width="600">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="goPage" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

    <!--编辑界面
        v-model：在1.X的时候是控制此标签是否展示，但是2.X之后是双向绑定
        :rules="editFormRules"：表单校验规则绑定那个规则对象
          prop="name"：此字段运用表单校验规则中的那个规则
    -->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典标签" prop="username">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典名字" prop="phone">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>


<!--  抽屉编辑界面  -->
    <el-dialog :title="title2" :visible.sync="editFormVisible2" :close-on-click-modal="false">
      <el-form :model="editForm2" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item :label="title3" prop="username">
          <el-input  v-model="editForm2.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="字典类型" prop="phone">
          <el-input :disabled="true" v-model="editForm2.dictionaryType.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="字典类型id" prop="types_id">
          <el-input :disabled="true" v-model="editForm2.dictionaryType.id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible2=false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit2" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉代码-->
    <el-drawer
        :title="title3"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table @row-dblclick="getClick" :data="gridData" highlight-current-row v-loading="listLoading2" @selection-change="selsChange" style="width: 100%;">

        <!--这是elementui提供的选择框，此选择框可以多选-->
        <el-table-column type="selection" width="55">
        </el-table-column>

        <el-table-column type="index" width="60" label="编号">
        </el-table-column>
        <el-table-column prop="name" :label="title3" width="120" >
        </el-table-column>
        <el-table-column prop="dictionaryType.name"  label="字典类型" width="100" >
        </el-table-column>
        <el-table-column label="操作" width="150">

          <template scope="scope">
            <el-button size="small" @click="handleEdit2(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel2(scope.$index, scope.row)">删除</el-button>
          </template>
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="handleAdd2(scope.$index, scope.row)">新增</el-button>
          </template>
        </el-table-column>
        <el-table-column>

        </el-table-column>

      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <!--
          disabled：是否不可点击
            true：不可点击
            false：可点击
        -->
        <el-button type="danger" @click="batchRemove2" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination
            layout="total, sizes, prev, pager, next"
            @current-change="handleCurrentChange"
            :current-page="currentPage2"
            :page-size="pageSize2"
            :total="total2"
            @size-change="handleSizeChange"
            :page-sizes="[5, 10, 15, 30, 50]"
            style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

  </section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
      return {

        table: false,
        gridData: [],
        filters: {
          name: ''
        },
        rowes:null,
        dictionarys: [],
        total: 0,
        total2: 0,
        typeId:0,
        currentPage: 1,
        currentPage2: 1,
        listLoading: false,
        listLoading2: false,
        pageSize: 5,
        pageSize2: 5,
        sels: [],//列表选中列
        title:"",
        title2:"",
        title3:"",
        editFormVisible: false,//编辑界面是否显示
        editFormVisible2: false,
        editLoading: false,
        editLoading2: false,
        // 表单校验规则
        editFormRules: {
          name: [
            // 为空校验
            {required: true, message: '请输入部门名称', trigger: 'blur'}
          ],
          sn: [
            // 为空校验
            {required: true, message: '请输入部门编号', trigger: 'blur'}
          ]
        },

        //编辑界面数据
        editForm: {
          id:null,
          sn:"",
          name:""


        },

        //编辑界面数据
        editForm2: {
          id:null,
          name:"",
          dictionaryType:{}


        },
        // employees:[]
      }
    },

		methods: {
      gettypeId(){
        let name=this.title3
        this.$http.get("/dictionary/a/"+name)
          .then(result=>{
            this.typeId=parseInt(result.data)
            console.log(result.data)
          })
          .catch(result=>{

          })

      },
      getDetil(){
        let para = {
          currentPage: this.currentPage2,
          pageSize: this.pageSize2,
          id:this.typeId
        };
        this.table=true
        this.listLoading2 = true;
        this.$http.post("/detail",para)
            .then(result=>{
              this.listLoading2=false
              result=result.data
              this.total2=result.total
              this.gridData=result.rows
              this.table=true

            }).catch(result=>{})
      },

      getDetil1(row){
        let para = {
          currentPage: this.currentPage2,
          pageSize: this.pageSize2,
          id: row.id
        };
        this.table=true
        this.listLoading2 = true;
        this.$http.post("/detail",para)
            .then(result=>{
              this.listLoading2=false
              result=result.data
              this.total2=result.total
              this.gridData=result.rows
              this.table=true
              this.gettypeId();
            }).catch(result=>{})
      },
      getClick(row){

        // 根据当前行数据的主键Id，发起请求获取到当前行对应的数据，进行赋值
              this.rowes=row;
              this.title3=row.name
              this.getDetil1(row)


      },
      search(){
        // 如果是高级查询，为了避免用户的数据不够展示，我们需要初始化页码为1，不管有没有其他页数据，我们默认展示第一页
        this.currentPage = 1;
        // 页面初始化完成之后，直接调用查询列表数据函数即可
        this.getDictionaryType();
      },

      handleCurrentChange(val){
        this.currentPage2=val
        this.getDetil1(this.rowes);
      },
      handleSizeChange(val){
        this.pageSize2=val
        this.getDetil1(this.rowes);
      },
			goPage(val) {
				this.currentPage = val;
				this.getDictionaryType();
			},
			//获取员工列表
      getDictionaryType() {
				let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = false;
        this.$http.post("/dictionary",para)
          .then(result=>{
            this.listLoading=false;
            result=result.data
            this.total=result.total
            this.dictionarys=result.rows;

          })
          .catch(result=>{
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })

			},
			//删除
			handleDel: function (index, row) {

				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;

          this.$http.delete("/dictionary/"+row.id)
            .then(result=>{
              this.listLoading = false;
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              this.currentPage=1
              this.getDictionaryType()
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
				}).catch(() => {

				});
			},	//删除
			handleDel2: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;

          this.$http.delete("/detail/"+row.id)
            .then(result=>{
              this.listLoading = false;
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }

              this.handleCurrentChange(1)
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
				}).catch(() => {

				});
			},
			//显示编辑界面

      handleEdit: function (index, row) {
        this.title = "编辑";
        // 1.弹出编辑框
        this.editFormVisible = true;
        // 2.克隆对象到我们模型层的对象中
        this.editForm = Object.assign({}, row);
     },

      handleEdit2: function (index, row) {
        this.title2 = "编辑";
        // 1.弹出编辑框
        this.editFormVisible2 = true;
        // 2.克隆对象到我们模型层的对象中
        this.editForm2 = Object.assign({}, row);
        console.log()
     },
			//显示新增界面
			handleAdd: function () {
        this.title="新增"
        this.editFormVisible = true;

        this.editForm = {
          id:null,
          sn:"",
          name:""

        };
			},
      //显示新增明细界面
			handleAdd2: function () {
        this.title2="新增"
        this.editFormVisible2 = true;
        this.editForm2 = {

            id:null,
            name:"",
          dictionaryType:{
              id:this.typeId,
              name:this.title3
            }




        };
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
                this.$http.put("/dictionary",para)
                    .then(result=>{

                      // 1.关闭按钮忙等
                      this.editLoading = false;
                      // 2.关闭模态框
                      this.editFormVisible = false;
                      if(result.data.success){ // 新增/修改成功
                        this.$message({
                          message: result.data.message,
                          type: 'success'
                        });
                      }else { // 不一定新增/修改成功，我们也不知道新增/修改成功没有
                        this.$message({
                          message: result.data.message,
                          type: 'error'
                        });
                      }
                      this.currentPage=1
                      this.getDictionaryType();
                    })
                    .catch(result=>{
                      this.editLoading = false;
                      this.$message({
                        message: '系统繁忙，请重试！',
                        type: 'error'
                      });
                    })


						});
					}
				});
			},
      editSubmit2: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading2 = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm2);

                this.$http.put("/detail",para)
                    .then(result=>{

                      // 1.关闭按钮忙等
                      this.editLoading2 = false;
                      // 2.关闭模态框
                      this.editFormVisible2 = false;
                      if(result.data.success){ // 新增/修改成功
                        this.$message({
                          message: result.data.message,
                          type: 'success'
                        });
                      }else { // 不一定新增/修改成功，我们也不知道新增/修改成功没有
                        this.$message({
                          message: result.data.message,
                          type: 'error'
                        });
                      }
                      this.handleCurrentChange(1)
                    })
                    .catch(result=>{
                      this.editLoading2 = false;
                      this.$message({
                        message: '系统繁忙，请重试！',
                        type: 'error'
                      });
                    })


						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
      //批量删除
      batchRemove: function () {
        // 获取用户选中行的所有Id
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          // 开启列表忙等
          this.listLoading = true;
          // 发送Axios请求
          this.$http.patch("/dictionary", ids)
              .then(result=>{
                // 1.关闭忙等框
                this.listLoading = false;
                if(result.data.success){ // 绝对删除成功
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else { // 不一定删除成功，我们也不知道删除成功没有
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                // 3.改变当前页页码
                this.currentPage = 1;
                // 4.调用查询列表函数
                this.getDictionaryType();
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })

        }).catch(() => {

        });
      }, //批量删除
      batchRemove2: function () {
        // 获取用户选中行的所有Id
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          // 开启列表忙等
          this.listLoading = true;
          // 发送Axios请求
          this.$http.patch("/detail", ids)
              .then(result=>{
                // 1.关闭忙等框
                this.listLoading = false;
                if(result.data.success){ // 绝对删除成功
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else { // 不一定删除成功，我们也不知道删除成功没有
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.handleCurrentChange(1)
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })

        }).catch(() => {

        });
      },
      getShop(){
        this.$http.get("/shop")
          .then(result=>{
            this.shop=result.data
            console.log(this.shop)

          })
          .catch(result=>{
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })
      }
		},
		mounted() {
			this.getDictionaryType();
      this.getShop();

		}
	}

</script>

<style scoped>

</style>